#!/usr/bin/env python

import urllib2
from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def authed(config):
	if "anubis_user" and "anubis_pass" in config:
		return True
	return False

def submit(filepath, FROM_ADDR, USER=None, PASS=None):
	
	try:
		filehandle = open(filepath, "rb")
	except IOError, e:
		print "Error: " + e.strerror
		print "Could not open " + filepath + "for reading"
		sys.exit(1)
	
	# This registers the poster module with urllib2
	register_openers()
	
	# Build the POST request
	url = "http://anubis.iseclab.org/?action=analyze"
	params = { "analysisType" : "file",
				"executable" : filehandle,
				"notification" : "email",
				"email" : FROM_ADDR,
				"advanced" : "Submit for Analysis" }
	
	if USER and PASS:
		params['username'] = USER
		params['password'] = PASS

	datagen, headers = multipart_encode(params)
	request = urllib2.Request(url, datagen, headers)
	#request.set_proxy("127.0.0.1:8080", "http")

	# Make the request
	try:
		response = urllib2.urlopen(request)
		result = response.read()
		print "Anubis: Check your email for results"
	except urllib2.URLError, e:
		print e.reason
		print "Anubis: Malware submission FAILED"
		
	filehandle.close()
